package br.com.fiap.si;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * @author Kesley Miranda - Jonantan Sousa - Vitor Ferreira
 * 
 */
public class GPrincipal {

	private JFrame frame;
	

	private Graph<Integer, String> grafo = new SparseMultigraph<Integer, String>();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GPrincipal window = new GPrincipal();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @wbp.parser.entryPoint
	 */
	public GPrincipal() {
		
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		frame = new JFrame();
		frame.setBounds(100, 100, 780, 650);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 750, 600);
		frame.getContentPane().add(panel);
		
		
		ViewLerArquivo lerRM = new ViewLerArquivo();

		List<String> lista = new ArrayList<String>();
		List<Integer> vertex = new ArrayList<Integer>();

		lista = lerRM.lerArquivo();
		// fazer aqui o substring da linha da lista
		Integer i = 0;

		while (lista.size() > i) {

			Integer a = Integer.parseInt(lista.get(i).substring(0, 5));
			vertex.add(a);
			grafo.addVertex(a);

			i++;
		}
		i = 0;
		while (lista.size() > i) {


			Integer a = Integer.parseInt(lista.get(i).substring(6, 11));
			grafo.addEdge("- " + vertex.get(i) + " <-> " + a, vertex.get(i),
					a);
			if (lista.get(i).length() > 11) {
			Integer b = Integer.parseInt(lista.get(i).substring(12, 17));
			grafo.addEdge("- " + vertex.get(i) + " <-> " + b, vertex.get(i),
					b);
			if (lista.get(i).length() > 17) {
				Integer c = Integer.parseInt(lista.get(i).substring(18, 23));
				grafo.addEdge("- " + vertex.get(i) + " <-> " + c,
						vertex.get(i), c);
				if (lista.get(i).length() > 23) {
					Integer d = Integer
							.parseInt(lista.get(i).substring(24, 29));
					grafo.addEdge("- " + vertex.get(i) + " <-> " + d,
							vertex.get(i), d);
					if (lista.get(i).length() > 29) {
						Integer e = Integer.parseInt(lista.get(i).substring(30,
								35));
						grafo.addEdge("- " + vertex.get(i) + " <-> " + e,
								vertex.get(i), e);
					}
				}
			}}
			i++;
		}
		
		CircleLayout<Integer, String> layout = new CircleLayout<Integer, String>(
				grafo);

        layout.setSize(new Dimension(700,600));

		final VisualizationViewer<Integer, String> component = new VisualizationViewer <Integer, String>(
				layout);

		 component.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Integer>());
		
		component.setPreferredSize(new Dimension(750, 600));
		component.setVisible(true);

		panel.add(component);
		
		JButton btnNewButton = new JButton("Iniciar");
		component.add(btnNewButton);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				System.out.println("botao Presionado");
				//Menu p = new Menu();
				Menu.main(null);
			}
		});
		
		
	}
	
}
